//
//  FindViewController.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/5/27.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "FindViewController.h"
#import "RequestManager.h"
#import "FindMainTableViewCell.h"
#import "FindMainTableViewModel.h"
#import "SearchView.h"
#import "Header.h"
#import "ScrollViewModel.h"
#import "ButtonModel.h"
#import "ButtonView.h"
#import "FindNearMainViewController.h"
#import "FindHotViewController.h"


static NSString *cellIdentify = @"cell";
@interface FindViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong) NSMutableArray *listArrayTwo;
@property (nonatomic, strong) NSMutableArray *listArrayThree;
@property (nonatomic, strong) NSMutableArray *picArray;
@property (nonatomic, strong) NSMutableArray *scrolModelArray;
@property (nonatomic, strong) NSMutableArray *buttonArray;
@property (nonatomic, strong) NSMutableArray *buttonModelArray;
@property (nonatomic, strong) UIView *bigView;
@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.listArray = [NSMutableArray array];
    self.listArrayTwo = [NSMutableArray array];
    self.listArrayThree = [NSMutableArray array];
    self.picArray = [NSMutableArray array];
    self.scrolModelArray = [NSMutableArray array];
    self.buttonArray = [NSMutableArray array];
    self.buttonModelArray = [NSMutableArray array];
    
//    self.navigationController.navigationBar.hidden = YES;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight + 44) style:UITableViewStyleGrouped];
    
    [self.view addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [RequestManager requestWithUrl:kFindMainViewUrl requestType:RequestTypeGET parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *cardsArray = dic[@"cards"];
        NSDictionary *cardsDicPic = cardsArray[0];
        NSArray *card_groupPic = cardsDicPic[@"card_group"];
        NSDictionary *card_groupPicDic = card_groupPic[0];
        NSArray *pic_items = card_groupPicDic[@"pic_items"];
        for (NSDictionary *dic in pic_items) {
            ScrollViewModel *model = [[ScrollViewModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.scrolModelArray addObject:model];
            [self.picArray addObject:model.pic];
        }
        
        NSDictionary *cardsDicButton = cardsArray[1];
        NSArray *card_groupButton = cardsDicButton[@"card_group"];
        NSDictionary *card_groupDicButton = card_groupButton[0];
        NSArray *groupButton = card_groupDicButton[@"group"];
        for (NSDictionary *dic in groupButton) {
            ButtonModel *model = [[ButtonModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.buttonModelArray addObject:model];
            [self.buttonArray addObject:model.title_sub];
        }
        
        NSDictionary *cardsDic = cardsArray[2];
        NSArray *card_group = cardsDic[@"card_group"];
        for (NSDictionary *dic in card_group) {
            FindMainTableViewModel *model = [[FindMainTableViewModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.listArray addObject:model];
        }
        
        NSDictionary *cardsDicTwo = cardsArray[3];
        NSArray *card_groupTwo = cardsDicTwo[@"card_group"];
        for (NSDictionary *dic in card_groupTwo) {
            FindMainTableViewModel *model = [[FindMainTableViewModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.listArrayTwo addObject:model];
        }
        
        NSDictionary *cardsDicThree = cardsArray[4];
        NSArray *card_groupThree = cardsDicThree[@"card_group"];
        for (NSDictionary *dic in card_groupThree) {
            FindMainTableViewModel *model = [[FindMainTableViewModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.listArrayThree addObject:model];
        }
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [self addHeadView];
        });
        
    } error:nil];
    
    SearchView *searchView = [[SearchView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 30)];
//    self.navigationController.navigationBar.hidden = YES;
    self.navigationItem.titleView = searchView;
    
    
    
    
    
    
    [self.tableView registerClass:[FindMainTableViewCell class] forCellReuseIdentifier:cellIdentify];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor cyanColor];
}

- (void)addHeadView{
    self.bigView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 172 + 20 + 60)];
    Header *headerView = [[Header alloc]initWithFrame:CGRectMake(0, 0, kWidth, 172) createScrollViewWithPicArray:_picArray];
    [_bigView addSubview:headerView];
    
    ButtonView *buttonView = [[ButtonView alloc]initWithFrame:CGRectMake(0, 190, kWidth, 60)];
    [buttonView.buttonOne setTitle:self.buttonArray[0] forState:UIControlStateNormal];
    [buttonView.buttonTwo setTitle:self.buttonArray[1] forState:UIControlStateNormal];
    [buttonView.buttonThree setTitle:self.buttonArray[2] forState:UIControlStateNormal];
    [buttonView.buttonFour setTitle:self.buttonArray[3] forState:UIControlStateNormal];
    [buttonView.buttonOne setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonView.buttonTwo setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonView.buttonThree setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonView.buttonFour setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    buttonView.backgroundColor = [UIColor whiteColor];
    [_bigView addSubview:buttonView];
    self.tableView.tableHeaderView = _bigView;
    _tableView.tableFooterView = nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.listArray.count;
    }else if (section == 1){
        return self.listArrayTwo.count;
    }else{
        return self.listArrayThree.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FindMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.model = _listArray[indexPath.row];
        
    }else if (indexPath.section == 1){
        cell.model = _listArrayTwo[indexPath.row];
    }else{
        cell.model = _listArrayThree[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        if (indexPath.row == 2) {
            FindNearMainViewController *nearVC = [[FindNearMainViewController alloc]init];
            [self.navigationController pushViewController:nearVC animated:YES];
        }
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            FindHotViewController *findHotVC = [[FindHotViewController alloc]init];
            [self.navigationController pushViewController:findHotVC animated:YES];
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
